<?php

/**
 * import actions.
 *
 * @package    astolfo
 * @subpackage import
 * @author     Emerson Estrella
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class importActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    //$this->forward('default', 'module');
    
  	$this->setLayout(false);
  	
  	$monitors = Doctrine::getTable('TweetMonitor')->findAll();
  	foreach($monitors as $m){
  	
  		$url = "http://search.twitter.com/search.atom?rpp=100&nocache=".time()."&q=".urlencode($m->getQuery());
  		echo "\n\n>>>>".$url;
  		$i=0;
  	
  		$contents = file_get_contents($url);
  		$xml = new SimpleXMLElement($contents);
  		foreach($xml->entry as $item) {
  	
  			$image = "";
  			$tweet_url = "";
  			if(!$t = Doctrine::getTable('Tweet')->findOneByTwitterId($item->id)) {
  				foreach($item->link as $link){
  					if($link["rel"] == "image"){
  						$image = $link["href"];
  					}
  					elseif($link["rel"] == "alternate"){
  						$tweet_url = $link["href"];
  					}
  				}
  				$t = new Tweet();
  				$t->setTwitterId($item->id);
  				$t->setTweetMonitorId($m->getId());
  				$t->setContent(str_replace('href', 'target="_blank" href', $item->content));
  				$t->setAvatar($image);
  				$t->setUrl($tweet_url);
  				$t->setAuthor($item->author->name);
  				$t->setAuthorUrl($item->author->uri);
  				$t->setDatePublished($item->published);
  				if($item['id'] != "tag:search.twitter.com,2005:8524984970"){
  					$t->save();
  					$i++;
  				}
  			}
  		}
  		echo "\n".$i." tweets adicionados";
  	
  	}
  	
  	//mail('emersonestrella@gmail.com', 'Twitter Monitor', 'TV Cultura: Twitter monitor!');
  	die('FIM!');
  	
  }
}
